package repository

import (
	"context"
	"database/sql"
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/service/forum/model"

	"github.com/sirupsen/logrus"
	"github.com/volatiletech/null/v8"
	"github.com/volatiletech/sqlboiler/v4/boil"
	"github.com/volatiletech/sqlboiler/v4/queries/qm"
	"github.com/zeromicro/go-zero/core/stores/cache"
)

type (
	// ReactionRepository is an interface to be customized, add more methods here,
	// and implement the added methods in customReactionRepository.
	ReactionRepository interface {
		FindOne(ctx context.Context, id uint64) (*model.ForumReaction, error)
		FindOneWithExpire(
			ctx context.Context,
			id uint64,
			expire time.Duration,
		) (*model.ForumReaction, error)
		FindOneNoCache(
			ctx context.Context,
			conn boil.ContextExecutor,
			id uint64,
		) (*model.ForumReaction, error)
		GetList(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.ForumReaction, int64, error)
		Insert(
			ctx context.Context,
			tx boil.ContextExecutor,
			data *model.ForumReaction,
			rel *model.ForumReactionRel,
		) error
		Update(
			ctx context.Context,
			tx boil.ContextExecutor,
			data *model.ForumReaction,
			rel *model.ForumReactionRel,
		) error
		UpdateBatch(ctx context.Context, tx boil.ContextExecutor, ids []uint64, cols model.M) error
		SoftDelete(ctx context.Context, tx boil.ContextExecutor, id uint64) error
		Restore(ctx context.Context, tx boil.ContextExecutor, id uint64) error
		GetMedia(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.ForumAttachmentRelationship, int64, error)
		GetMeta(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.ForumReactionMetum, int64, error)
		InsertLog(ctx context.Context, tx boil.ContextExecutor, data *model.ForumReactionLog) error // ##25.01.03##
		GetLogs(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.ForumReactionLog, int64, error) // ##25.01.03##
	}

	customReactionRepository struct {
		modelx.CachedConn
		mysqlConn           *sql.DB
		cacheIdPrefix       string
		cacheIdExpirePrefix string
	}
)

// NewReactionRepository returns a repository for the database table.
func NewReactionRepository(mysqlConn *sql.DB, cacheConf cache.CacheConf) ReactionRepository {
	return &customReactionRepository{
		CachedConn:          modelx.NewConn(mysqlConn, cacheConf),
		mysqlConn:           mysqlConn,
		cacheIdPrefix:       "cache:forum:reactions:id:",
		cacheIdExpirePrefix: "cache:forum:reactions:id:expire:",
	}
}

func (r *customReactionRepository) FindOne(
	ctx context.Context,
	id uint64,
) (*model.ForumReaction, error) {
	cacheIdKey := r.formatPrimary(id)
	var resp model.ForumReaction
	err := r.QueryCtx(ctx, &resp, cacheIdKey, func(ctx context.Context, conn *sql.DB, v any) error {
		data, err := model.FindForumReaction(ctx, conn, id)
		if err != nil {
			return err
		}

		*v.(*model.ForumReaction) = *data

		return nil
	})

	switch err {
	case nil:
		return &resp, nil
	case sql.ErrNoRows:
		return nil, model.ErrNotFound
	default:
		return nil, err
	}
}

func (r *customReactionRepository) FindOneWithExpire(
	ctx context.Context,
	id uint64,
	expire time.Duration,
) (*model.ForumReaction, error) {
	cacheIdKey := r.formatPrimary(id)
	var resp model.ForumReaction
	err := r.QueryWithExpireCtx(ctx, &resp, cacheIdKey, expire, func(ctx context.Context, conn *sql.DB, v any) error {
		data, err := model.FindForumReaction(ctx, conn, id)
		if err != nil {
			return err
		}

		*v.(*model.ForumReaction) = *data

		return nil
	})

	switch err {
	case nil:
		return &resp, nil
	case sql.ErrNoRows:
		return nil, model.ErrNotFound
	default:
		return nil, err
	}
}

func (r *customReactionRepository) FindOneNoCache(
	ctx context.Context,
	conn boil.ContextExecutor,
	id uint64,
) (*model.ForumReaction, error) {
	var db boil.ContextExecutor = r.mysqlConn
	if conn != nil {
		db = conn
	}
	data, err := model.FindForumReaction(ctx, db, id)
	logrus.Info(fmt.Sprintf("FindOneNoCache err: %+v", err))
	switch err {
	case nil:
		return data, nil
	case sql.ErrNoRows:
		return nil, model.ErrNotFound
	default:
		return nil, err
	}
}

func (r *customReactionRepository) GetList(
	ctx context.Context,
	cols model.M,
	withCount int, // -1-返回list；0-返回count；1-返回count和list；2-根据offset返回count和list；3-根据preMinId返回count和list；4-根据preMaxId返回count和list
	filter map[string]map[string][]any,
) (list []*model.ForumReaction, count int64, err error) {
	var page int
	var pageSize int
	var limit int
	var selectCol string
	var mods []qm.QueryMod
	var nextCount int64
	var previousCount int64
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(uint64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(uint64); ok && val > 0 {
				pageSize = int(val)
			}
		case "limit":
			if val, ok := v.(uint32); ok && val > 0 {
				limit = int(val)
			}
		case "offset":
			if withCount == 2 {
				if val, ok := v.(uint32); ok && val > 0 {
					mods = append(mods, qm.Offset(int(val)))
					previousCount = 1
				}
			}
		case "preMinId":
			if withCount == 3 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionColumns.ReactionID+" DESC"))
					mods = append(mods, model.ForumReactionWhere.ReactionID.LT(val))
					previousCount = 1
				}
			}
		case "preMaxId":
			if withCount == 4 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionColumns.ReactionID+" ASC"))
					mods = append(mods, model.ForumReactionWhere.ReactionID.GT(val))
					previousCount = 1
				}
			}
		case "orderBy":
			if withCount != 3 && withCount != 4 {
				if val, ok := v.(string); ok && val != "" {
					mods = append(mods, qm.OrderBy(val))
				}
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.ForumReactionColumns.ReactionID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionID, opMap)...)
			case model.ForumReactionColumns.ForumID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ForumID, opMap)...)
			case model.ForumReactionColumns.TopicID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.TopicID, opMap)...)
			case model.ForumReactionColumns.PostID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.PostID, opMap)...)
			case model.ForumReactionColumns.CommemtID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.CommemtID, opMap)...)
			case model.ForumReactionColumns.AuthorID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.AuthorID, opMap)...)
			case model.ForumReactionColumns.ReactionUserID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionUserID, opMap)...)
			case model.ForumReactionColumns.ReactionUser:
				mods = append(mods, modelx.GetStringMods(model.ForumReactionColumns.ReactionUser, opMap)...)
			case model.ForumReactionColumns.ReactionUserLevel:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionUserLevel, opMap)...)
			case model.ForumReactionColumns.ReactionUserAnonymous:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionUserAnonymous, opMap)...)
			case model.ForumReactionColumns.ReactionType:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionType, opMap)...)
			case model.ForumReactionColumns.ReactionStatus:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionStatus, opMap)...)
			case model.ForumReactionColumns.ReactionTypeCount:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionColumns.ReactionTypeCount, opMap)...)
			}
		}
	}

	if withCount >= 2 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit+1))
		}
	} else if withCount >= 0 {
		count, err = model.ForumReactions(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}

		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	} else {
		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		} else {
			columns = append(columns, model.ForumReactionColumns.ReactionID)
		}
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.ForumReactions(mods...).All(ctx, r.mysqlConn)
	if withCount >= 2 {
		if limit > 0 && len(list) > limit {
			nextCount = 1
			list = list[:limit]
		}
		count = previousCount + nextCount*2
	}

	return
}

func (r *customReactionRepository) Insert(
	ctx context.Context,
	tx boil.ContextExecutor,
	data *model.ForumReaction,
	rel *model.ForumReactionRel,
) error {
	err := r.ExecCtx(ctx, func(ctx context.Context, conn *sql.DB) error {
		var db boil.ContextExecutor
		if tx != nil {
			db = tx
		} else {
			db = conn
		}

		err := data.Insert(ctx, db, boil.Infer())
		if err != nil {
			return err
		}

		if rel == nil {
			return nil
		}

		reactionId := data.ReactionID
		if len(rel.Media) > 0 {
			for _, v := range rel.Media {
				v.ObjectID = reactionId
				v.ObjectType = uint16(globalkey.ForumAttachmentTypeReaction)
				err = v.Insert(ctx, db, boil.Infer())
				if err != nil {
					return err
				}
			}
		}

		return nil
	}, r.getCacheKeys(data)...)

	return err
}

func (r *customReactionRepository) Update(
	ctx context.Context,
	tx boil.ContextExecutor,
	data *model.ForumReaction,
	rel *model.ForumReactionRel,
) error {
	err := r.ExecCtx(ctx, func(ctx context.Context, conn *sql.DB) error {
		var db boil.ContextExecutor
		if tx != nil {
			db = tx
		} else {
			db = conn
		}

		rowsAff, err := data.Update(ctx, db, boil.Blacklist(
			model.ForumReactionColumns.ReactionID,
			model.ForumReactionColumns.ForumID,
			model.ForumReactionColumns.ReactionUserID,
			model.ForumReactionColumns.ReactionDate,
			model.ForumReactionColumns.ReactionDateGMT,
			model.ForumReactionColumns.DeletedAt,
		))
		if err != nil {
			return err
		}

		if rowsAff == 0 {
			return model.ErrNotUpdated
		}

		if rel == nil {
			return nil
		}

		reactionId := data.ReactionID
		if len(rel.Media) > 0 {
			for _, v := range rel.Media {
				if v.AttachmentRelationshipID > 0 {
					_, err = v.Update(ctx, db, boil.Blacklist(
						model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID,
						model.ForumAttachmentRelationshipColumns.ObjectID,
						model.ForumAttachmentRelationshipColumns.ObjectType,
						model.ForumAttachmentRelationshipColumns.AttachmentID,
					))
					if err != nil {
						return err
					}
				} else {
					v.ObjectID = reactionId
					v.ObjectType = uint16(globalkey.ForumAttachmentTypeReaction)
					err = v.Insert(ctx, db, boil.Infer())
					if err != nil {
						return err
					}
				}
			}
		}

		if len(rel.DelMediaIds) > 0 {
			var mods []qm.QueryMod
			mods = append(mods, model.ForumAttachmentRelationshipWhere.AttachmentRelationshipID.IN(rel.DelMediaIds))
			_, err = model.ForumAttachmentRelationships(mods...).DeleteAll(ctx, db)
			if err != nil {
				return err
			}
		}

		return nil
	}, r.getCacheKeys(data)...)

	return err
}

func (r *customReactionRepository) UpdateBatch(
	ctx context.Context,
	tx boil.ContextExecutor,
	ids []uint64,
	cols model.M,
) error {
	if len(ids) > 0 {
		err := r.ExecCtx(ctx, func(ctx context.Context, conn *sql.DB) error {
			var db boil.ContextExecutor
			if tx != nil {
				db = tx
			} else {
				db = conn
			}

			var mods []qm.QueryMod
			mods = append(mods, model.ForumReactionWhere.ReactionID.IN(ids))

			rowsAff, err := model.ForumReactions(mods...).UpdateAll(ctx, db, cols)
			if err != nil {
				return err
			}

			if rowsAff == 0 {
				return model.ErrNotUpdated
			}

			return nil
		}, r.formatPrimaries(ids)...)

		return err
	}

	return nil
}

func (r *customReactionRepository) SoftDelete(
	ctx context.Context,
	tx boil.ContextExecutor,
	id uint64,
) error {
	data, err := r.FindOneNoCache(ctx, tx, id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil
		}

		return err
	}

	if data.ReactionStatus == uint8(globalkey.StatusDeleted) {
		return nil
	}

	data.DelStatus = data.ReactionStatus
	data.ReactionStatus = uint8(globalkey.StatusDeleted)
	data.DeletedAt = null.TimeFrom(time.Now())

	err = r.ExecCtx(ctx, func(ctx context.Context, conn *sql.DB) error {
		var db boil.ContextExecutor
		if tx != nil {
			db = tx
		} else {
			db = conn
		}

		rowsAff, err := data.Update(ctx, db, boil.Blacklist(
			model.ForumReactionColumns.ReactionID,
			model.ForumReactionColumns.ReactionModifiedDate,
			model.ForumReactionColumns.ReactionModifiedGMT,
		))
		if err != nil {
			return err
		}

		if rowsAff == 0 {
			return model.ErrNotUpdated
		}

		return nil
	}, r.getCacheKeys(data)...)

	return err
}

func (r *customReactionRepository) Restore(
	ctx context.Context,
	tx boil.ContextExecutor,
	id uint64,
) error {
	data, err := r.FindOneNoCache(ctx, tx, id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil
		}

		return err
	}

	if data.ReactionStatus != uint8(globalkey.StatusDeleted) {
		return nil
	}

	data.ReactionStatus = data.DelStatus
	data.DeletedAt = null.TimeFromPtr(nil)

	err = r.ExecCtx(ctx, func(ctx context.Context, conn *sql.DB) error {
		var db boil.ContextExecutor
		if tx != nil {
			db = tx
		} else {
			db = conn
		}

		rowsAff, err := data.Update(ctx, db, boil.Blacklist(
			model.ForumReactionColumns.ReactionID,
			model.ForumReactionColumns.ReactionModifiedDate,
			model.ForumReactionColumns.ReactionModifiedGMT,
		))
		if err != nil {
			return err
		}

		if rowsAff == 0 {
			return model.ErrNotUpdated
		}

		return nil
	}, r.getCacheKeys(data)...)

	return err
}

func (r *customReactionRepository) getCacheKeys(data *model.ForumReaction) []string {
	if data == nil {
		return []string{}
	}

	cacheIdKey := r.formatPrimary(data.ReactionID)
	cacheKeys := []string{
		cacheIdKey,
	}

	return cacheKeys
}

func (r *customReactionRepository) formatPrimary(primary any) string {
	return fmt.Sprintf("%s%v", r.cacheIdPrefix, primary)
}

func (r *customReactionRepository) formatPrimaries(primaries []uint64) []string {
	var resp []string
	for _, primary := range primaries {
		resp = append(resp, r.formatPrimary(primary))
	}

	return resp
}

func (r *customReactionRepository) queryPrimary(ctx context.Context, conn *sql.DB, v, primary any) error {
	var pid uint64
	if jsonId, ok := primary.(json.Number); ok {
		if int64Id, err := jsonId.Int64(); err == nil {
			pid = uint64(int64Id)
		} else {
			return err
		}
	}

	data, err := model.FindForumReaction(ctx, conn, pid)
	if err != nil {
		return err
	}

	*v.(*model.ForumReaction) = *data

	return nil
}

func (r *customReactionRepository) GetMedia(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.ForumAttachmentRelationship, count int64, err error) {
	var page int
	var pageSize int
	var limit int
	var selectCol string
	var mods []qm.QueryMod
	var nextCount int64
	var previousCount int64
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(uint64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(uint64); ok && val > 0 {
				pageSize = int(val)
			}
		case "limit":
			if val, ok := v.(uint32); ok && val > 0 {
				limit = int(val)
			}
		case "offset":
			if withCount == 2 {
				if val, ok := v.(uint32); ok && val > 0 {
					mods = append(mods, qm.Offset(int(val)))
					previousCount = 1
				}
			}
		case "preMinId":
			if withCount == 3 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID+" DESC"))
					mods = append(mods, model.ForumAttachmentRelationshipWhere.AttachmentRelationshipID.LT(val))
					previousCount = 1
				}
			}
		case "preMaxId":
			if withCount == 4 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID+" ASC"))
					mods = append(mods, model.ForumAttachmentRelationshipWhere.AttachmentRelationshipID.GT(val))
					previousCount = 1
				}
			}
		case "orderBy":
			if withCount != 3 && withCount != 4 {
				if val, ok := v.(string); ok && val != "" {
					mods = append(mods, qm.OrderBy(val))
				}
			}
		case "select":
			if withCount != 5 {
				if val, ok := v.(string); ok && val != "" {
					selectCol = val
				}
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID, opMap)...)
			case model.ForumAttachmentRelationshipColumns.ObjectID:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.ObjectID, opMap)...)
			case model.ForumAttachmentRelationshipColumns.ObjectType:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.ObjectType, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentID:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.AttachmentID, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentThumbnail:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.AttachmentThumbnail, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentOrder:
				mods = append(mods, modelx.GetIntMods(model.ForumAttachmentRelationshipColumns.AttachmentOrder, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentSource:
				mods = append(mods, modelx.GetStringMods(model.ForumAttachmentRelationshipColumns.AttachmentSource, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentImageURL:
				mods = append(mods, modelx.GetStringMods(model.ForumAttachmentRelationshipColumns.AttachmentImageURL, opMap)...)
			case model.ForumAttachmentRelationshipColumns.AttachmentImageAlt:
				mods = append(mods, modelx.GetStringMods(model.ForumAttachmentRelationshipColumns.AttachmentImageAlt, opMap)...)
			}
		}
	}

	if withCount >= 2 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit+1))
		}
	} else if withCount >= 0 {
		count, err = model.ForumAttachmentRelationships(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}

		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	} else {
		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		} else {
			columns = append(columns, model.ForumAttachmentRelationshipColumns.AttachmentRelationshipID)
		}
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.ForumAttachmentRelationships(mods...).All(ctx, r.mysqlConn)
	if withCount >= 2 {
		if limit > 0 && len(list) > limit {
			nextCount = 1
			list = list[:limit]
		}
		count = previousCount + nextCount*2
	}

	return
}

func (r *customReactionRepository) GetMeta(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.ForumReactionMetum, count int64, err error) {
	var page int
	var pageSize int
	var limit int
	var selectCol string
	var mods []qm.QueryMod
	var nextCount int64
	var previousCount int64
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(uint64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(uint64); ok && val > 0 {
				pageSize = int(val)
			}
		case "limit":
			if val, ok := v.(uint32); ok && val > 0 {
				limit = int(val)
			}
		case "offset":
			if withCount == 2 {
				if val, ok := v.(uint32); ok && val > 0 {
					mods = append(mods, qm.Offset(int(val)))
					previousCount = 1
				}
			}
		case "preMinId":
			if withCount == 3 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionMetumColumns.MetaID+" DESC"))
					mods = append(mods, model.ForumReactionMetumWhere.MetaID.LT(val))
					previousCount = 1
				}
			}
		case "preMaxId":
			if withCount == 4 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionMetumColumns.MetaID+" ASC"))
					mods = append(mods, model.ForumReactionMetumWhere.MetaID.GT(val))
					previousCount = 1
				}
			}
		case "orderBy":
			if withCount != 3 && withCount != 4 {
				if val, ok := v.(string); ok && val != "" {
					mods = append(mods, qm.OrderBy(val))
				}
			}
		case "select":
			if withCount != 5 {
				if val, ok := v.(string); ok && val != "" {
					selectCol = val
				}
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.ForumReactionMetumColumns.MetaID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.MetaID, opMap)...)
			case model.ForumReactionMetumColumns.UserID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.UserID, opMap)...)
			case model.ForumReactionMetumColumns.ForumID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.ForumID, opMap)...)
			case model.ForumReactionMetumColumns.TopicID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.TopicID, opMap)...)
			case model.ForumReactionMetumColumns.PostID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.PostID, opMap)...)
			case model.ForumReactionMetumColumns.CommemtID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.CommemtID, opMap)...)
			case model.ForumReactionMetumColumns.AuthorID:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.AuthorID, opMap)...)
			case model.ForumReactionMetumColumns.ReactionType:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.ReactionType, opMap)...)
			case model.ForumReactionMetumColumns.ReactionCount:
				mods = append(mods, modelx.GetIntMods(model.ForumReactionMetumColumns.ReactionCount, opMap)...)
			}
		}
	}

	if withCount >= 2 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit+1))
		}
	} else if withCount >= 0 {
		count, err = model.ForumReactionMeta(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}

		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	} else {
		if pageSize > 0 {
			if page > 1 {
				mods = append(mods, qm.Offset(pageSize*(page-1)))
			}
			mods = append(mods, qm.Limit(pageSize))
		}
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		} else {
			columns = append(columns, model.ForumReactionMetumColumns.MetaID)
		}
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.ForumReactionMeta(mods...).All(ctx, r.mysqlConn)
	if withCount >= 2 {
		if limit > 0 && len(list) > limit {
			nextCount = 1
			list = list[:limit]
		}
		count = previousCount + nextCount*2
	}

	return
}

func (r *customReactionRepository) InsertLog(
	ctx context.Context,
	tx boil.ContextExecutor,
	data *model.ForumReactionLog,
) error {
	var db boil.ContextExecutor
	if tx != nil {
		db = tx
	} else {
		db = r.mysqlConn
	}

	return data.Insert(ctx, db, boil.Infer())
}

func (r *customReactionRepository) GetLogs(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.ForumReactionLog, count int64, err error) {
	var page int
	var pageSize int
	var limit int
	var selectCol string
	var mods []qm.QueryMod
	var nextCount int64
	var previousCount int64
	var oldest string
	var latest string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(uint64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(uint64); ok && val > 0 {
				pageSize = int(val)
			}
		case "limit":
			if val, ok := v.(uint32); ok && val > 0 {
				limit = int(val)
			}
		case "offset":
			if withCount == 2 {
				if val, ok := v.(uint32); ok && val > 0 {
					mods = append(mods, qm.Offset(int(val)))
					previousCount = 1
				}
			}
		case "orderBy":
			if val, ok := v.(string); ok && len(val) > 0 {
				mods = append(mods, qm.OrderBy(val))
			}
		case "preMinId":
			if withCount == 3 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionLogColumns.ID+" DESC"))
					mods = append(mods, model.ForumReactionLogWhere.ID.LT(val))
					previousCount = 1
				}
			}
		case "preMaxId":
			if withCount == 4 {
				if val, ok := v.(uint64); ok && val > 0 {
					mods = append(mods, qm.OrderBy(model.ForumReactionLogColumns.ID+" ASC"))
					mods = append(mods, model.ForumReactionLogWhere.ID.GT(val))
					previousCount = 1
				}
			}
		case "select":
			if withCount != 5 {
				if val, ok := v.(string); ok && len(val) > 0 {
					selectCol = val
				}
			}
		case "oldest":
			if val, ok := v.(string); ok {
				oldest = val
			}
		case "latest":
			if val, ok := v.(string); ok {
				latest = val
			}
		}
	}

	for key, opMap := range filter {
		switch key {
		case model.ForumReactionLogColumns.ID:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.ID, opMap)...)
		case model.ForumReactionLogColumns.TopicID:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.TopicID, opMap)...)
		case model.ForumReactionLogColumns.PostID:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.PostID, opMap)...)
		case model.ForumReactionLogColumns.UserID:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.UserID, opMap)...)
		case model.ForumReactionLogColumns.Type:
			mods = append(mods, modelx.GetStringMods(model.ForumReactionLogColumns.Type, opMap)...)
		case model.ForumReactionLogColumns.Status:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.Status, opMap)...)
		case model.ForumReactionLogColumns.Count:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.Count, opMap)...)
		case model.ForumReactionLogColumns.CreatedAt:
			mods = append(mods, modelx.GetTimeMods(model.ForumReactionLogColumns.CreatedAt, opMap)...)
		case model.ForumReactionLogColumns.CreateDate:
			mods = append(mods, modelx.GetIntMods(model.ForumReactionLogColumns.CreateDate, opMap)...)
		}
	}

	if len(oldest) > 0 || len(latest) > 0 {
		if len(oldest) > 0 {
			mods = append(mods, qm.Select(fmt.Sprintf(
				"min(%s) as %s",
				model.ForumReactionLogColumns.ID,
				model.ForumReactionLogColumns.ID,
			)))
			switch oldest {
			case model.ForumReactionLogColumns.TopicID:
				mods = append(mods, qm.GroupBy(model.ForumReactionLogColumns.TopicID))
			case model.ForumReactionLogColumns.PostID:
				mods = append(mods, qm.GroupBy(model.ForumReactionLogColumns.PostID))
			default:
				return
			}
		} else {
			mods = append(mods, qm.Select(fmt.Sprintf(
				"max(%s) as %s",
				model.ForumReactionLogColumns.ID,
				model.ForumReactionLogColumns.ID,
			)))
			switch latest {
			case model.ForumReactionLogColumns.TopicID:
				mods = append(mods, qm.GroupBy(model.ForumReactionLogColumns.TopicID))
			case model.ForumReactionLogColumns.PostID:
				mods = append(mods, qm.GroupBy(model.ForumReactionLogColumns.PostID))
			default:
				return
			}
		}

		if limit > 0 {
			mods = append(mods, qm.Limit(limit+1))
		} else if pageSize > 0 {
			mods = append(mods, qm.Limit(pageSize))
		}

		preList, listErr := model.ForumReactionLogs(mods...).All(ctx, r.mysqlConn)
		if listErr != nil {
			err = listErr
			return
		}

		if len(preList) > 0 {
			if withCount >= 2 {
				if limit > 0 && len(preList) > limit {
					nextCount = 1
					preList = preList[:limit]
				}
				count = previousCount + nextCount*2
			}
			if len(preList) == 0 {
				return
			}

			ids := make([]uint64, 0, len(preList))
			for _, v := range preList {
				ids = append(ids, v.ID)
			}
			var subMods []qm.QueryMod
			subMods = append(subMods, model.ForumReactionLogWhere.ID.IN(ids))

			var columns []string
			if len(selectCol) > 0 {
				if selectCol == "," {
					columns = append(columns, model.ForumReactionLogColumns.ID)
				} else {
					columns = strings.Split(selectCol, ",")
				}
			}
			if len(columns) > 0 {
				subMods = append(subMods, qm.Select(columns...))
			}

			list, err = model.ForumReactionLogs(subMods...).All(ctx, r.mysqlConn)
			return
		}

		return
	}

	if withCount >= 2 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit+1))
		}
	} else if withCount >= 0 {
		count, err = model.ForumReactionLogs(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 && (withCount == -1 || withCount == 1) {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if len(selectCol) > 0 {
		if selectCol == "," {
			columns = append(columns, model.ForumReactionLogColumns.ID)
		} else {
			columns = strings.Split(selectCol, ",")
		}
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.ForumReactionLogs(mods...).All(ctx, r.mysqlConn)
	if err != nil {
		return
	}

	if withCount >= 2 {
		if limit > 0 && len(list) > limit {
			nextCount = 1
			list = list[:limit]
		}
		count = previousCount + nextCount*2
	}

	return
}
